package com.example.spark.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.example.spark.entity.Affair;
import com.example.spark.entity.dto.PaginationDTO;
import com.example.spark.entity.result.CommonResult;
import com.example.spark.service.AffairService;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

@CrossOrigin
@RestController
@RequestMapping("/affair")
public class AffairController {
    private final AffairService affairService;

    public AffairController(AffairService affairService) {
        this.affairService = affairService;
    }

    /**
     * 获取业务
     */
    @PostMapping("/getAllAffair")
    public CommonResult<IPage<Affair>> getAll(@RequestBody PaginationDTO<Affair> pageDto){
        return affairService.getAll(pageDto);
    }

    /**
     * 添加业务
     * @param affair 业务对象
     */
    @PostMapping("/saveAffair")
    public CommonResult<Affair> saveOne(@RequestBody Affair affair){
        return affairService.saveOne(affair);
    }

    /**
     * 修改图片
     */
    @PostMapping("/updateAffair/photo")
    public CommonResult<String> updatePhoto(@RequestParam MultipartFile photo, @RequestParam Long id){
        return affairService.updatePhoto(photo,id);
    }
    /**
     * 删除业务
     */
    @DeleteMapping("/deleteAffair")
    public CommonResult<Long> deleteById(@RequestParam Long id){
        return affairService.deleteById(id);
    }

    /**
     * 修改业务
     * @param affair 修改的业务信息
     */
    @PutMapping("/updateAffair")
    public CommonResult<Affair> updateOne(@RequestBody Affair affair){
        return affairService.updateOne(affair);
    }
}
